<template>
  <div>
    <headCpts />
    <mainCpts />
    <footCpts />
  </div>
</template>

<script setup>
import { $TipMessage } from "../http";
// import { getCurrentInstance } from "vue";
import headCpts from "../components/headCpts.vue";
import mainCpts from "../components/mainCpts.vue";
import footCpts from "../components/footCpts.vue";
import { useCounterStore } from "../store";
import WebRoomkit from "neroom-web-sdk";
import { onMounted, ref } from "vue";
import mitt from "mitt";
import { NIM } from "@yxim/nim-web-sdk";
const store = useCounterStore();
store.mittOb = mitt();

const { ipcRenderer } = require("electron");

import { loginYunXinApi, roomLiveApi, iMaccountApi } from "../http";

const IMaccount = ref(null);
const initIMinstance = () => {
  const myNim = store.enroomInstance.reuseIM(NIM);
  return new Promise((resolve) => {
    store.nimInstance = myNim.getInstance({
      debug: true,
      appKey: store.appKey,
      account: store.profile.user.id,
      token: IMaccount.value.statictoken,
      db: true,
      privateConf: {},
      onconnect: async (msg) => {
        await getInitRoomkitInstance();
      },
      onwillreconnect: (msg) => {},
      ondisconnect: (msg) => {
        switch (msg.code) {
          case 302:
            break;
          case 417:
            break;
          case "kicked":
            localStorage.clear();
            ipcRenderer.send("login-out");
            $TipMessage("你已被踢出", "error");
            break;
          default:
            break;
        }
      },
      onerror: (msg) => {
        console.log("onerror-msg", msg);
      },
      onroamingmsgs: (msg) => {
        console.log("onroamingmsgs-msg", msg);
      },
      onofflinemsgs: (msg) => {
        console.log("onofflinemsgs-msg", msg);
      },
      onmsg: (msg) => {
        console.log("onmsg-msg", msg);
        if (store.allMsg[msg.from]) {
          store.allMsg[msg.from].push(msg);
        } else {
          store.allMsg[msg.from] = [msg];
        }
      },
      onloginportschange: (msg) => {
        store.nimInstance.kick({
          deviceIds: ["deviceId1"],
          done: function onKick(err, obj) {
            console.log("kick-err", err);
            console.log("kick-obj", obj);
          },
        });
  
        $TipMessage("当前登录帐号在其它端登录", "error");
      },
    });
    store.nimInstance && resolve();
  });
};
const getIMaccount = async () => {
  try {
    let data = await iMaccountApi();
    if (data.data.errcode === 0) {
      IMaccount.value = data.data.data;
    }
    console.log(" IMaccount.value ", IMaccount.value);
    await initIMinstance();
  } catch (er) {
    console.log("er", er);
  }
};
// enroom
const getInitRoomkitInstance = async () => {
  try {
    console.log("store.nimInstance", store.nimInstance);

    let initRes = await store.enroomInstance.initialize({
      appKey: store.appKey, 
      im: store.nimInstance, 
      logLevel: "debug", 
    });
    console.log("initRes", initRes);
    store.NEAuthService = store.enroomInstance.authService;
    store.NERoomService = store.enroomInstance.roomService;
    store.NEAuthService.addAuthListener(listener);
    await getLoginYunXin();
  } catch (er) {
    console.log("er", er);
  }
};
const listener = (code) => {
  switch (code) {
    case 0:
      console.log("watch-addAuthListener-----------------: KICK_OUT");
      break;
    case 1:
      console.log("watch-addAuthListener: UNAUTHORIZED");
      break;
    case 2:
      console.log("watch-addAuthListener: FORBIDDEN");
      break;
    case 3:
      console.log("watch-addAuthListener: ACCOUNT_TOKEN_ERROR");
      break;
    case 4:
      console.log("watch-addAuthListener: LOGGED_IN");
      break;
    case 5:
      console.log("watch-addAuthListener: LOGGED_OUT");
      break;
    case 1026:
      console.log("watch-addAuthListener: TOKEN_EXPIRED");
      break;
    case 1027:
      console.log("watch-addAuthListener: TOKEN_INCORRECT");
      break;
    case 10119:
      console.log("watch-addAuthListener: TOKEN_INCORRECT");
      break;
  }
};
const getLoginYunXin = async () => {
  try {
    let data = await loginYunXinApi(store.profile.jwt);
    let myToken = data.data.data.user.neroom_token;
    console.log("account", store.profile.user.id, "myToken", myToken);
    let login = await store.NEAuthService.login(store.profile.user.id, myToken);

    console.log("loginYunXin-res", login);
    await getRoomLive();
  } catch (err) {
    console.log("loginYunXin-err", err);
  }
};
const getRoomLive = async () => {
  0.0;
  try {
    let data = await roomLiveApi(store.profile.jwt);
    console.log("获取直播房间", data);
    store.roomLiveInfo = data.data.list[0];
    console.log(" store.roomLiveInfo", store.roomLiveInfo);
    store.pkRoomInfo = data.pkroom;
  } catch (err) {
    console.log("roomLiveApi-err", err);
  }
};
onMounted(async () => {
  if (!store.profile) {
    ipcRenderer.send("login-out");
  }
  store.enroomInstance = new WebRoomkit();
  console.log("store.enroomInstance", store.enroomInstance);
  await getIMaccount();
});
</script>
<style scoped lang="scss"></style>
